namespace com.stifel.BranchMgmt
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;

    using com.stifel.Extensions.SharePointExtensions;

    using Microsoft.SharePoint;

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid( "fd64bb5d-7b88-4efa-9512-778a55e0d0f4" )]
    public class BranchManagementWebLevelBranchParentEventReceiver : SPFeatureReceiver
    {
        #region Fields

        const string SITE = "branchsystems";

        #endregion Fields

        #region Methods

        public override void FeatureActivated( SPFeatureReceiverProperties properties )
        {
            if ( !isCorrectSite( properties ) )
                throw new Exception( string.Format( "The feature cannot be activated in this website. To activate this feature, the website must be the '{0}' website.", SITE ) );

            using ( StifelBranchTypes_Definition o = new StifelBranchTypes_Definition( properties ) )
                o.Create( );

            using ( StifelHardwareTypes_Definition o = new StifelHardwareTypes_Definition( properties ) )
                o.Create( );

            using ( StifelPhotoType_Definition o = new StifelPhotoType_Definition( properties ) )
                o.Create( );

            using ( StifelBranchPersonnelType_Definition o = new StifelBranchPersonnelType_Definition( properties ) )
                o.Create( );

            using ( StifelVendor_Definition o = new StifelVendor_Definition( properties ) )
                o.Create( );
        }

        public override void FeatureDeactivating( SPFeatureReceiverProperties properties )
        {
            if ( !isCorrectSite( properties ) )
                throw new Exception( string.Format( "The feature cannot be activated in this website. To activate this feature, the website must be the '{0}' website.", SITE ) );

            using ( StifelBranchTypes_Definition o = new StifelBranchTypes_Definition( properties ) )
                o.Delete( );

            using ( StifelHardwareTypes_Definition o = new StifelHardwareTypes_Definition( properties ) )
                o.Delete( );

            using ( StifelPhotoType_Definition o = new StifelPhotoType_Definition( properties ) )
                o.Delete( );

            using ( StifelBranchPersonnelType_Definition o = new StifelBranchPersonnelType_Definition( properties ) )
                o.Delete( );

            using ( StifelVendor_Definition o = new StifelVendor_Definition( properties ) )
                o.Delete( );
        }

        bool isCorrectSite( SPFeatureReceiverProperties properties )
        {
            Uri uri = new Uri( properties.GetWebUrl( ) );
            string[ ] s = uri.Segments;
            List<string> k = s.ToList( );
            List<string> l = new List<string>( );
            k.ForEach( a => l.Add( a.ToLower( ).Trim( '/' ) ) );
            return l[ l.Count - 1 ].Equals( SITE );
        }

        #endregion Methods

        #region Other

        // Uncomment the method below to handle the event raised after a feature has been installed.
        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}
        // Uncomment the method below to handle the event raised before a feature is uninstalled.
        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}
        // Uncomment the method below to handle the event raised when a feature is upgrading.
        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}

        #endregion Other
    }
}